<?php
require_once('Vega/Dao/Table.php');

class Mobitv_Admin_PackageDao extends Vega_Dao_Table
{
    public function getDetail($packageId)
    {
        $packageId = intval($packageId);

        $sql = "SELECT * FROM package WHERE id=$packageId";
        $this->_db->query($sql);

        return $this->_db->fetchRow();
    }
    
    public function getList()
    {
        $sql = "SELECT * FROM package";
        $this->_db->query($sql);

        return $this->_db->fetchRowset();
    }

    public function insert($data)
    {
        $name = $this->_db->valueQuote($data['name']);
        $price = $this->_db->valueQuote($data['price']);
        $description = $this->_db->valueQuote($data['description']);

        $sql = "INSERT INTO package SET name=$name, description=$description, price=$price, updated_by=".$_SESSION['userid'].", updated_time=now()";

        return $this->_db->queryWrite($sql);
    }

    public function save($packageId, $data)
    {
        $packageId = intval($packageId);
        $name = $this->_db->valueQuote($data['name']);
        $description = $this->_db->valueQuote($data['description']);

        $sql = "UPDATE package SET name=$name, description=$description, updated_by=".$_SESSION['userid'].", updated_time=now() WHERE id=$packageId";

        return $this->_db->queryWrite($sql);
    }

    public function deleteFromList($list)
    {
        $list = "(" . implode(",", $list) . ")";
        // Xoa kenh-goi
        $sql = "DELETE FROM package_channel WHERE package_id IN $list";
        $this->_db->queryWrite($sql);
        // Xoa goi
        $sql = "DELETE FROM package WHERE id IN $list";
        return $this->_db->queryWrite($sql);
    }
}